// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(cc.Node)
    rotateTable: cc.Node = null;

    @property(cc.Node)
    rotateBtn: cc.Node = null;

    distance: number = 0;
    speed: number = 0; //速度
    startLocation = 0; //开始位置
    drop = [10000, 1000, 500, 200, 100, 50, -1, 0];  // [1, 3, 5, 10, 11, 15, 25, 30];
    weight = [1, 4, 9, 24, 40, 60, 85, 100];
    startTime = 0; //开始时间
    timer = 0; //时长
    inTouch = false;
    giftIndex = 0; //奖品位置
    chance = 1;
    money = 0;
    inRotate = false;
    start () {
        this.rotateBtn.on(cc.Node.EventType.TOUCH_START, (touch,event) => {
            //记录位置
            if(this.inTouch) return;
            this.inTouch = true;
            this.startLocation = touch.getLocationY();
            this.startTime = Date.now();
        })
        this.rotateBtn.on(cc.Node.EventType.TOUCH_END, (touch,event) => {
            if(this.chance <= 0) {
                this.nodes.note.getComponent(cc.Label).string = "请购买抽奖次数";
                return ;
            }
            if(this.inRotate) {
                this.nodes.note.getComponent(cc.Label).string = "请等待抽奖结果";
                return ;
            }
            this.distance = touch.getLocationY() - this.startLocation;
            if(this.distance >= 0) {
                this.speed = 0;
                return;
            }
            this.distance = Math.abs(this.distance);
            cc.log(Date.now());
            this.timer = Date.now() - this.startTime;
            this.inTouch = false;
            this.speed = this.distance / (this.timer/20);
            //获取奖品
            this.getGift();
            this.rotate();
        })
        this.touchEndEventScale(this.nodes.buy, () => {
            if(this.inRotate || this.chance >= 1) {
                this.nodes.note.getComponent(cc.Label).string = "请等待结果";
                return ;
            }
            if(game.playerMgr.calMoney(-100)){

                this.chance += 1;
                this.nodes.note.getComponent(cc.Label).string = "请转动转盘";
                game.playerMgr.achieveFinish('gamble', 1);
            } 
        })

    }
    getGift() {
        this.chance --;
        let num = Math.random() * 100;
        let tmpArr = this.weight.concat(num);
        tmpArr.sort((a, b) => a - b);
        this.giftIndex = tmpArr.findIndex(val => val == num);
        if(this.giftIndex == 6) {
            this.chance += 1;
            return ;
        }
        this.money = this.drop[this.giftIndex];
    }
    rotate() {
        this.inRotate = true;
        if(this.speed >= 5) {
            this.speed = 5;
        } else if(this.speed <= 1) {
            this.speed = 1;
        }
        cc.tween(this.rotateTable)
        .by(10/this.speed, {angle: -1080 - this.giftIndex * 45}, { easing: 'sineOut'})
        .call(() => {
           
            if(this.giftIndex == 6) {
                this.nodes.note.getComponent(cc.Label).string = "恭喜，获得一次机会";
            } else {
                this.nodes.note.getComponent(cc.Label).string = "恭喜获得大奖" + this.money + "元";
                if(this.money > 0) {
                    game.recordMgr.addList({name: "你", action: `获得${this.money}的奖励`, location: "东华赌场，转盘赌博中，运气爆炸"});
                }
                game.playerMgr.calMoney(this.money);
            }
            this.scheduleOnce(() => {this.rotateTable.angle = -22; this.inRotate = false;}, 1.5);
            
        })
        .start();
    }
}
